import '@tarojs/async-await';
import Taro, { Component, Config } from '@tarojs/taro'
import { Provider } from '@tarojs/mobx'

import Index from './pages/index'
import {getRuntime, ServerHost} from './utils/common'

// import counterStore from './store/counter'
import Stores from './store/Stores';

import './app.less'
import 'taro-ui/dist/style/index.scss' // 引入组件样式

// 如果需要在 h5 环境中开启 React Devtools
// 取消以下注释：
// if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5')  {
//   require('nerv-devtools')
// }

const store = {
  stores: new Stores()
};

class App extends Component {
  componentDidMount () {
    // 当用户通过分享二维码进来时，用户授权之后的毁掉地址是首页
    Taro.setNavigationBarTitle({ title: '建务通' })
    // console.log(getRuntime().getUserInfo())
    let url = window.location.href
    let code = ''
    if(url.includes('code')){
      code = window.location.href.split('=')[1]
    }
    // 获取用户信息, 判断用户是否关注公众号
    let user = getRuntime().getUserInfo()
    let token = user&&user.token ? 'Bearer '+user.token : ''
    if(code && getRuntime().getToken()){
      let fetchUrl = ServerHost+'/api/Account/getWchatUserInfo?code='+code
      fetch(fetchUrl, {
        method: 'GET',
        headers: {
          'Authorization': token
        },
      }).then(response => {
        return response.json()
      }).then( res=>{
        if(res.subscribe === '0'){ // 未关注
          getRuntime().redirectTo('/pages/login/qr_code')
          return
        }
      }).catch(e => {
        console.error(e)
      })
    }
  }

  config: Config = {
    pages: [
      "pages/index/index", //首页
      "pages/index/notice_detail", //首页
      "pages/index/demand", //首页-发布需求
      "pages/index/services", //首页-提供服务
      "pages/index/position", //首页-发布职位
      "pages/index/rresume", //首页-发布简历
      "pages/legal/index", //律政中心-首页
      "pages/legal/detail", //律政中心-律师详情
      "pages/finance/index", //工程金融中心-首页
      "pages/finance/page1", //工程金融中心-工程保函中心
      "pages/finance/page1_step_two", //工程金融中心-工程保函中心
      "pages/finance/page1_step_three", //工程金融中心-工程保函中心d
      "pages/finance/finance_success", //工程金融中心-提交成功
      "pages/finance/page2", //工程金融中心-工程现金
      "pages/finance/page3", //工程金融中心-工程项目预约
      "pages/finance/page4", //工程金融中心-其他工程服务中心
      "pages/finance/page4_demand", //工程金融中心-其他工程服务中心-发布项目
      "pages/finance/page4_detail", //工程金融中心-其他工程服务中心-项目详情
      "pages/finance/financeQuery", //工程金融中心-保函查询
      "pages/business/index", //工商服务中心-首页
      "pages/business/page1", //工商服务中心-工商服务代注册
      "pages/business/business_success", //工商服务中心-预约成功
      "pages/business/page2", //工商服务中心-公司报税、年检、注销
      "pages/business/page2_declare", //工商服务中心-公司报税、年检、注销-报税
      "pages/business/page2_inspection", //工商服务中心-公司报税、年检、注销-年检
      "pages/business/page2_cancel", //工商服务中心-公司报税、年检、注销-注销
      "pages/business/page3", //工商服务中心-建企资质服务
      "pages/business/page3_detail", //工商服务中心-建企资质服务-详情
      "pages/business/page3_demand", //工商服务中心-建企资质服务-发布信息
      "pages/business/page4", //工商服务中心-建其他服务
      "pages/business/page4_water", //工商服务中心-其他服务-水利安全达标
      "pages/business/page4_credit", //工商服务中心-其他服务-水利信用达标
      "pages/business/page4_highway", //工商服务中心-其他服务-公路安全达标
      "pages/library/index", //建务库中心
      "pages/company_credit/index", //企业信用中心
      "pages/company_credit/page1", //企业信用中心-IOS三大体系
      "pages/company_credit/page2", //企业信用中心-3A信用评级
      //"pages/personnel/index", //工程人才中心-首页
      //"pages/personnel/detail", //工程人才中心-人才详情
      "pages/dynamic/index", //动态-首页
      "pages/dynamic/detail", //动态-详情
      "pages/public_welfare/index", //公益-首页
      "pages/public_welfare/activity", //公益-活动列表
      "pages/public_welfare/activity_detail", //公益-活动详情
      "pages/public_welfare/welfare", //公益-公益列表
      "pages/public_welfare/welfare_detail", //公益-公益详情
      "pages/public_welfare/welfare_apply", //公益-申请
      "pages/public_welfare/welfare_success", //公益-申请成功
      "pages/public_welfare/welfare_record", //公益-我的资助记录
      "pages/public_welfare/welfare_record_detail", //公益-我的资助记录-记录详情
      "pages/user/index", //我的-首页
      "pages/user/my_profit", //我的-我的收益
      "pages/user/my_profit_list", //我的-我的收益-收益明细
      "pages/user/my_profit_cash_none", //我的-我的收益-收益明细
      "pages/user/cash_now", //我的-我的收益-立即提现
      "pages/user/cash_success", //我的-我的收益-立即提现-提现成功
      "pages/user/sign", //我的-早起打卡
      "pages/user/team", //我的-我的团队
      "pages/user/team_member", //我的-我的团队-下级成员
      "pages/user/integral", //我的-我的积分
      "pages/user/apply_log", //我的-我的申请
      "pages/user/upgrade", //我的-会员升级
      "pages/user/upgrade_pay_success", //我的-会员升级
      "pages/user/buy_log", //我的-会员升级-购买记录
      "pages/user/qrcode", //我的-我的二维码
      "pages/user/bank_card", //我的-我的银行卡
      "pages/user/bank_card_add", //我的-我的银行卡-添加
      "pages/user/address", //我的-收货地址
      "pages/user/address_add", //我的-收货地址-添加、编辑
      "pages/user/address_edit", //我的-收货地址-编辑
      "pages/user/help", //我的-帮助中心
      "pages/user/help_detail", //我的-帮助中心-详情
      "pages/user/course", //我的-我的课程
      "pages/user/course_detail", //我的-我的课程-详情
      "pages/user/order", //我的-我的订单
      "pages/user/user_info_edit", //我的-编辑用户资料
      // "pages/user/order_detail_unpay", //我的-我的订单-订单详情-未付款
      "pages/user/order_detail", //我的-我的订单-详情
      "pages/user/order_pay_success",
      "pages/user/setting", //我的-设置
      "pages/user/set_phone", //我的-设置手机号
      "pages/user/set_password", //我的-设置密码
      "pages/user/set_password_phone", //我的-设置密码
      "pages/user/about", //我的-关于我们
      "pages/user/company_auth", //我的-企业认证
      "pages/user/collection", //我的-我的收藏
      "pages/user/issue", //我的-我的发布
      "pages/user/company_address", //我的-关于我们-公司位置
      "pages/business_school/index", //建企商学院-首页
      "pages/business_school/course_list", //建企商学院-首页-查看更多
      "pages/business_school/detail", //建企商学院-详情
      "pages/business_school/build_company", //建企商学院-建企类
      "pages/business_school/architect", //建企商学院-建造师
      "pages/business_school/other", //建企商学院-其他人员
      "pages/tendering/index", //招投标中心
      "pages/all_services/index", //全部服务
      "pages/all_services/none", //全部服务-建企OA办公-无
      "pages/all_services/life", //全部服务-生活服务中心-生活服务中心
      "pages/mall/index", //积分商城
      "pages/mall/detail", //积分商城-商品详情
      "pages/mall/order_confirm", //积分商城-详情-确认订单
      "pages/mall/pay_success", //积分商城-详情-确认订单-支付成功
      "pages/mall/custom", //积分商城-礼品定制中心
      "pages/mall/custom_list", //积分商城-礼品定制中心-列表
      "pages/mall/custom_detail", //积分商城-礼品定制中心-列表-详情
      "pages/mall/order_confirm_custom", //积分商城-礼品定制中心-订单确认
      "pages/mall/address", //积分商城-收货地址
      "pages/login/login", //登录页
      "pages/login/register_step_one", // 注册-第一步
      "pages/login/register_step_two", // 注册-第二步
      "pages/login/forget_password", // 忘记密码
      "pages/login/qr_code", // 关注微信公众号
      "pages/other/report_detail", // 举报
      "pages/other/submit_success", // 表单提交成功
      "pages/other/report_success", // 举报提交成功
      "pages/other/regist_agreement", // 用户注册协议
      "pages/newest/index", // 最新消息
      "pages/newest/personnel_center", // 工程人才中心
      "pages/newest/demand_detail", // 最新消息-需求详情
      "pages/newest/service_detail", // 最新消息-服务详情
      "pages/newest/position_detail", // 最新消息-职位详情
      "pages/newest/personnel_detail", // 最新消息-人才详情
    ],
    window: {
      backgroundTextStyle: "light",
      navigationBarBackgroundColor: "#ffffff",
      navigationBarTitleText: "建务通",
      navigationBarTextStyle: "black",
      backgroundColor: '#ffffff',
      onReachBottomDistance: 10
    },
  };

  componentDidCatchError () {}

  render () {
    return (
      <Provider store={store}>
        <Index />
      </Provider>
    )
  }
}

Taro.render(<App />, document.getElementById('app'))
